<?php
/**
 * CreateTime: 2024/1/2 16:39
 * User:LinQ
 */

namespace App\Http\Controllers\Sip;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\MeLawCase\MeLawCase;
use App\Models\MeLawCase\MeLawCaseBindMeDetails;
use App\Models\MeLawCase\MeLawCasePhoneModel;
use App\Models\MeSip\MeSipModel;
use App\Models\Staff\StaffModel;
use App\Tools\JhWh\JhWhClient;
use Illuminate\Support\Facades\Storage;


class SipController
{
    public function sipCallBack(){
        $res = request()->all();
        if(is_array($res)){
            $res = json_encode($res);
        }
        $fmtRes = json_decode($res,true);
        $lyUrl = "";
        if($fmtRes['RecordFile']){
            $lyUrl = stripslashes($fmtRes['FileServer']).":8082/".stripslashes($fmtRes['RecordFile']);
        }
        (new MeSipModel())->add([
            'sip_id' => $fmtRes['ActionID'],
            'state' => $fmtRes['State'],
            'ly_url' => $lyUrl,
            'call_data' => $res
        ]);
    }

    public function callSip($caseId){
        $caseModel = new MeLawCase();
        $casePhoneId = $caseModel->getById($caseId,['main_respondent_phone_id']);
        if(!$casePhoneId){
            return ResponseFunc::returnJson(
                'error',
                null,
                '呼叫失败号码错误',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
        $casePhoneModel = new MeLawCasePhoneModel();
        $phoneInfo = $casePhoneModel->getById($casePhoneId['main_respondent_phone_id']);
        // 获取当前这个用户的外呼id
        $userInfo = (new StaffModel())->select(['staff_seats_number'])->where('staff_id',TokenFuc::getCurrentStaffInfo()['staff_id'])->first();
        if(!$userInfo['staff_seats_number']){
            return ResponseFunc::returnJson(
                'error',
                null,
                '坐席未绑定',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
        $res = (new JhWhClient())->callDaiLout($userInfo['staff_seats_number'],$phoneInfo['phone_number']);
        $fmtRes = json_decode($res,true);
        if($fmtRes['Succeed']==false){
            return ResponseFunc::returnJson(
                'error',
                null,
                '外呼异常请稍后重试'.$fmtRes['Message'],
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }

        $res = (new MeLawCaseBindMeDetails())->add([
            'law_case_id' => $caseId,
            'me_details_uuid' => $fmtRes['ActionID']
        ]);
        return ResponseFunc::returnJson(
            'ok',
            [
                'id' => $fmtRes['ActionID']
            ],
            '操作成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function getSipInfo($actionID){
        $lawCaseDetailModel = new MeSipModel();
        $info = $lawCaseDetailModel->where('sip_id',$actionID)->first();
        $data = null;
        if($info){
            $info = $info->toArray();
            $fmt = json_decode($info['call_data'],true);
            $fmt['call_url'] = $info['ly_url'] ?? "";
//            if($fmt['call_url']){
//                Storage::disk()->put(stripslashes($fmt['RecordFile']),file_get_contents($fmt['call_url']));
//            }
            $data['call_url'] = $info['ly_url'];
            $data['callee'] = $fmt['CalledNo'] ?? "-";
            $data['caller'] = $fmt['CallNo'] ?? "-";
            $data['callType'] = $fmt['CallType'] ?? "-";
            $data['callTypeStr'] = "-";
            $data['createTime'] = $fmt['Ring'];
            $data['answenTime'] = $fmt['Begin'];
            $data['endTime'] = $fmt['End'];
            $data['extensionNumber'] = $fmt['Agent'];
            $data['talkDuration'] = strtotime($data['endTime'])-strtotime($data['answenTime']);
            $data['callDuration '] = "-";
            switch ($data['callType']){
                case 'dialout':
                    $data['callTypeStr'] = "外呼通话";
                    break;
                case 'normal':
                    $data['callTypeStr'] = "普通来电";
                    break;
                case 'transfer':
                    $data['callTypeStr'] = "转接电话";
                    break;
                case 'dialTransfer':
                    $data['callTypeStr'] = "外呼转接";
                    break;
            }
        }
        return ResponseFunc::returnJson(
            'ok',
            $data,
            '操作成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }
}